package recipe;

public class Ingredient {
	/*
	 * 1 cup = 8 fl oz
	 * 1 pint = 2 cups
	 * 1 quart = 2 pints
	 * 4 cups = 1 quart
	 * 1 gallon = 4 quarts
	 * 
	 * 16 oz = 1 lb
	 * ***********************
	 * 
	 * standard = metric
	 * 
	 * fluid oz = mL, multiply oz by 30 to get mL
	 * gal = L, multiply gal by 3.8 to get L
	 * qt = L, multiply qt by 0.9 to get L
	 * 
	 * oz = g, multiply oz by 28 to get g
	 * lb = kg, multiply by 0.45 to get kg
	 * 
	 */
	
	public static final String UNIT_QUANTITY = "";
	
	public static final String UNIT_METRIC_ML = "mL";
	public static final String UNIT_METRIC_L = "L";
	public static final String UNIT_METRIC_GRAM = "g";
	public static final String UNIT_METRIC_KILOGRAM = "kg";
	
	public static final String UNIT_STANDARD_FL_OZ = "fl oz";
	public static final String UNIT_STANDARD_CUP = "c";
	public static final String UNIT_STANDARD_PINT = "pt";
	public static final String UNIT_STANDARD_QUART = "qt";
	public static final String UNIT_STANDARD_GALLON = "gal";
	public static final String UNIT_STANDARD_TEASPOON = "tsp";
	public static final String UNIT_STANDARD_TABLESPOON = "Tbsp";
	public static final String UNIT_STANDARD_OZ = "oz";
	public static final String UNIT_STANDARD_POUND = "lb";
	
	// Ingredient attributes
	private String ingredientName;
	private String ingredientAmount;
	private String ingredientUnit;
	private boolean isMetric;
	
	/**
	 * Default constructor for Ingredient class
	 */
	public Ingredient(){
		ingredientName = null;
		ingredientAmount = null;
		ingredientUnit = null;
		isMetric = false;
	} // constructor
	
	/**
	 * Overloaded constructor for Ingredient class
	 * 
	 * Description: Initializes data fields for Ingredient
	 *     Assumes all fields are correct
	 * @param name
	 * @param amount
	 * @param unit
	 */
	public Ingredient(String name, String amount, String unit){
		ingredientName = name;
		ingredientAmount = amount;
		ingredientUnit = unit;
		isMetric = isUnitMetric(unit);
	} // constructor
	
	/**
	 * 
	 * @override
	 */
	public String toString(){
		return 
			ingredientAmount + " " + 
			ingredientUnit + " " +
			ingredientName;
	}
	
	/**
	 * 
	 * @param unit
	 * @return
	 */
	public static boolean validateUnit(String unit){
		return 
			unit.equals(UNIT_METRIC_ML) ||
			unit.equals(UNIT_METRIC_L) ||
			unit.equals(UNIT_METRIC_GRAM) ||
			unit.equals(UNIT_METRIC_KILOGRAM) ||
			unit.equals(UNIT_STANDARD_CUP) ||
			unit.equals(UNIT_STANDARD_FL_OZ) ||
			unit.equals(UNIT_STANDARD_GALLON) ||
			unit.equals(UNIT_STANDARD_OZ) ||
			unit.equals(UNIT_STANDARD_PINT) ||
			unit.equals(UNIT_STANDARD_POUND) ||
			unit.equals(UNIT_STANDARD_QUART) ||
			unit.equals(UNIT_STANDARD_TABLESPOON) ||
			unit.equals(UNIT_STANDARD_TEASPOON) ||
			unit.equals(UNIT_QUANTITY);
	}
	
	/**
	 * 
	 * @param amount
	 * @return
	 */
	public static double parseAmount(String amount){
		double num = 0, den = 0;
		String[] fraction = null;
		double value = 0;
		boolean isMatch = false;
		boolean isFraction = false;
		boolean isDecimal = false;
		boolean isInteger = false;
		
		// Integer - one or more digits
		isInteger = amount.matches("\\d++");
		// Decimal number - one or more digits 
		// followed by a period followed by 
		// one or more digits
		isDecimal = amount.matches("\\d++\\.\\d++");
		// Fraction - one or more digits 
		// followed by a '/' followed by 
		// one or more digits
		isFraction = amount.matches("\\d++/\\d++");
		
		isMatch = isInteger || isDecimal || isFraction;	
		
		if(isMatch){
			if(isInteger){
				value = Integer.parseInt(amount);
			}
			else if(isDecimal){
				value = Double.parseDouble(amount);
			}
			else if(isFraction){
				fraction = amount.split("/");
				num = Double.parseDouble(fraction[0]);
				den = Double.parseDouble(fraction[1]);
				value = num / den;
			}
		}
		
		return value;
	}
	
	/**
	 * 
	 * @return
	 */
	public static String[] getUnits(){
		String[] units = {
			UNIT_QUANTITY,
			UNIT_METRIC_ML,
			UNIT_METRIC_L,
			UNIT_METRIC_GRAM,
			UNIT_METRIC_KILOGRAM,
			UNIT_STANDARD_FL_OZ,
			UNIT_STANDARD_CUP,
			UNIT_STANDARD_PINT,
			UNIT_STANDARD_QUART,
			UNIT_STANDARD_GALLON,
			UNIT_STANDARD_TEASPOON,
			UNIT_STANDARD_TABLESPOON,
			UNIT_STANDARD_OZ,
			UNIT_STANDARD_POUND
		};
		
		return units;
	}
	
	private boolean isUnitMetric(String unit){
		return unit.equals(UNIT_METRIC_L) || unit.equals(UNIT_METRIC_ML) ||
			unit.equals(UNIT_METRIC_GRAM) || unit.equals(UNIT_METRIC_KILOGRAM);
	}
	
	public void setIngredientName(String name){
		ingredientName = name;
	}
	
	public String getIngredientName(){
		return ingredientName;
	}
	
	public void setAmount(String amount){
		ingredientAmount = amount;
	}
	
	public String getAmount(){
		return ingredientAmount;
	}
	
	public void setUnit(String unit){
		ingredientUnit = unit;
	}
	
	public String getUnit(){
		return ingredientUnit;
	}
}
